package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;

import com.huaweicloud.sdk.iotda.v5.model.ProductSummary;
import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author Jiagege
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备管理相关接口")
@Slf4j
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备管理列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 同步产品列表---------放置到缓存中
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        log.error("同步产品列表开始");
        deviceService.syncProductList();
        return success("同步设备数据成功");
    }

    /**
     * 查询所有产品列表----这里读取redis缓存的数据，因为同步数据那一步直接把数据已经放置到缓存中
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        List<ProductVo> allProduct = deviceService.allProduct();
        return success(allProduct);
    }

    /**
     * 注册设备----新增
     * @param deviceDto
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDevice(deviceDto);
        return success("注册设备成功");
    }

    /**
     * 获取设备详细信息-----回显
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    public AjaxResult getInfo(@PathVariable("iotId") String iotId) {
        DeviceDetailVo deviceDetail = deviceService.queryDeviceDetail(iotId);
        return success(deviceDetail);
    }


    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("获取设备数据")
    public AjaxResult queryServiceProperties(@PathVariable String iotId) {
        List<Map<String, Object>> resultList = deviceService.getServiceInfo(iotId);
        return success(resultList);
    }

    @PutMapping
    @ApiOperation("修改设备")
    public AjaxResult updateDevice(@RequestBody DeviceDto dto) {
        deviceService.updateIotDevice(dto);
        return success("修改设备成功");
    }


    @DeleteMapping("/{iotId}")
    @ApiOperation("删除设备")
    public AjaxResult deleteDeviceByIotId(@PathVariable String iotId) {
        deviceService.deleteDeviceByIotId(iotId);
        log.error("删除设备成功!!!!!!!");
        return success("删除设备成功！");
    }


    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("根据产品key查询报警规则")
    public AjaxResult queryProduct( @PathVariable String productKey)
    {
        log.info("根据产品key查询报警规则, productKey: {}", productKey);
        List<ServiceCapability> list= deviceService.queryProduct(productKey);
        return success(list);
    }

}
